﻿/**
 * Licensed to Open-Ones Group under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Open-Ones Group licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
namespace IPresenter
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Threading;
    using InTheHand.Net;
    using InTheHand.Net.Bluetooth;
    using InTheHand.Net.Sockets;
   
    /// <summary>
    /// This class provides functions to work with bluetooth
    /// </summary>
    public class BluetoothSupport
    {
        /// <summary>
        /// turn on/off bluetooth device inorder for others device may discover it.
        /// </summary>
        /// <param name="available">
        /// Action that user want to perform
        /// available = true : turn on
        /// available = false : turn off
        /// </param>
        /// <returns>True if succeed, false if error occurs.</returns>
        public static bool SetAvailable(bool available)
        {
            BluetoothRadio bltRadio = BluetoothRadio.PrimaryRadio;

            // if user want to turn on Bluetooth
            if (available)
            {
                if (bltRadio == null)
                {
                    return false;
                }

                // Set discoverable mode so that other device can discover bluetooth on this PC
                bltRadio.Mode = RadioMode.Discoverable;
                return true;
            }
            else
            {
                // turn off bluetooth
                if (bltRadio == null)
                {
                    return false;
                }

                bltRadio.Mode = RadioMode.PowerOff;
                return true;
            }
        }
        
        /// <summary>
        /// Receive message from another bluetooth device
        /// </summary>
        /// <param name="readerDevice">BluetoothClient that use to receive message</param>
        /// <param name="readerAdapter">Stream Reader that use to receive message</param>
        /// <returns>thông tin nhận được</returns>
        public static string Read(BluetoothClient readerDevice, StreamReader readerAdapter)
        {
            if (readerDevice.Connected)
            {
                // char hold size of received data
                char[] size = new char[1];

                // char array hold data content.
                char[] receivedData;
                try
                {
                    readerAdapter.Read(size, 0, 1);
                    receivedData = new char[size[0]];
                    readerAdapter.Read(receivedData, 0, size[0]);
                    return new string(receivedData);
                }
                catch (Exception ex)
                {
                    // if exception occurs
                    Logs.WriteErrorMessage(ex.Message);
                    return null;
                }
            }
            else
            {
                // if connection does'nt exist
                return null; 
            }
        }

        /// <summary>
        /// Send message to another bluetooth device
        /// </summary>
        /// <param name="writerDevice"> BluetoothClient that use to send message </param>
        /// <param name="writerAdapter"> StreamWriter that use to send message </param>
        /// <param name="message"> Message which you would like to send</param>
        /// <returns>
        /// 1: if success.
        /// 0: if unsuccess.
        /// </returns>
        public static int Write(BluetoothClient writerDevice, StreamWriter writerAdapter, string message)
        {
            if (writerDevice.Connected)
            {
                try
                {
                    writerAdapter.Write(message);
                    return 1;
                }
                catch (Exception ex)
                {
                    Logs.WriteErrorMessage(ex.Message);

                    ////send data error
                    return 0;
                }
            }
            else
            {
                // not connected yet
                return -1;
            }
        } 
    }
}
